package com.st.controller.headend;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.st.entity.OrderGoods;
import com.st.entity.ProductCategory;
import com.st.service.IOrderService;
import com.st.service.IProductCategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("myOrder")
public class MyOrderController {
    @Autowired
    private IOrderService orderService;
    @Autowired
    private IProductCategoryService productCategoryService;
    @RequestMapping("list")
    public ModelAndView list(OrderGoods orderGoods, HttpServletRequest req){
        //判断页码变量是否为空，如果为空就赋值1 表示第一页
        if (orderGoods.getPageNum() == null) {
            orderGoods.setPageNum(1);
        }
        //一个查5条
        orderGoods.setPageSize(5);
        //分页查询，注意该方法会对后面的第一个查询做分页
        PageHelper.startPage(orderGoods.getPageNum(), orderGoods.getPageSize());
        HttpSession session= req.getSession();
       Integer userId= (Integer) session.getAttribute("userId");
       String userPhone= (String) session.getAttribute("userPhone");
       orderGoods.setUserId(userId);
        ModelAndView mv=new ModelAndView();
        List<OrderGoods>list=orderService.queryMyOrder(orderGoods);
        PageInfo pageInfo = new PageInfo(list);
        mv.addObject("pageInfo",pageInfo);
        mv.addObject("orderGoods",orderGoods);
        mv.addObject("userPhone",userPhone);
        mv.setViewName("/headend/myOrder");
        return mv;
    }
    @RequestMapping("del")
    @ResponseBody
    public String del(String orderNo){
      Integer aa=orderService.del(orderNo);
      Integer bb=orderService.delOrder(orderNo);
      if (aa==1 && bb==1){
          return "1";
      }else {
          return "0";
      }
    }
    @RequestMapping("refund")
    @ResponseBody
    public String refund(String orderNo){
        Integer aa= orderService.goodsrefund(orderNo);
        if (aa==1){
            return "1";
        }else {
            return "0";
        }
    }
}
